﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setting_users : SessionPage
{
	protected int PageSize = 15;
	protected int PageStart = 0;
	protected int PageNum = 1;
	protected int CurPage = 1;

	LogicLayer mgr = new LogicLayer();
	List<SYS_User> objList = new List<SYS_User>();

    protected void Page_Load(object sender, EventArgs e)
    {
		//检查权限
		this.VerifyFunction("setting");

		//删除人员
		if (!string.IsNullOrEmpty(Request.QueryString["userID"]))
		{
			int disabled = 0;
			if (int.TryParse(Request.QueryString["disabled"], out disabled))
			{
				mgr.UpdateUser(Request.QueryString["userID"], disabled);
			}
		}

		//得到人员列表
		objList = mgr.ListSYS_User();

		//分页参数
		if (Request.QueryString["p"] != null)
		{
			try
			{
				CurPage = int.Parse(Request.QueryString["p"]);
			}
			catch { }
		}
		PageStart = (CurPage - 1) * PageSize;
		if (objList != null)
		{
			if (objList.Count % PageSize == 0)
				PageNum = objList.Count / PageSize;
			else
				PageNum = objList.Count / PageSize + 1;
		}
		else
		{
			PageNum = 0;
			PageStart = 0;
		}
    }

	protected void WriteTableData()
	{
		if (objList != null)
		{
			for (int i = PageStart; i < objList.Count && i < PageStart + PageSize; i++)
			{
				Response.Write("<tr>");

				Response.Write("<td nowrap>");
				Response.Write(objList[i].UserName);
				Response.Write("</td>");
				Response.Write("<td nowrap>");
				SYS_Employee empl = mgr.GetEmployeeByUserName(objList[i].UserName);
				if(empl != null)
					Response.Write("<a href=\"javascript:viewEmpl('" + empl.EmplID + "')\" >" +  empl.EmplName + "</a>" );
				else
					Response.Write("<a href=\"javascript:chooseEmpl('" + objList[i].UserName  + "')\" >...</a>");
				Response.Write("</td>");

				Response.Write("<td width=\"1%\" nowrap >");
				if (objList[i].Disabled == 0)
					Response.Write("<a href=\"javascript:toggleUser('" + objList[i].UserName + "',1)\">禁用</a>");
				else
					Response.Write("<a href=\"javascript:toggleUser('" + objList[i].UserName + "',0)\">启用</a>");
				Response.Write("</td>");

				Response.Write("</tr>");
			}
		}
	}

	protected void WriteTablePager()
	{
		if (PageNum > 0)
		{
			//页码数
			int LinkNum = 10;

			Response.Write("第");

			int PageNumX = 1, CurPageX = 1;
			if (PageNum % LinkNum == 0)
				PageNumX = PageNum / LinkNum;
			else
				PageNumX = PageNum / LinkNum + 1;

			if (CurPage % LinkNum == 0)
				CurPageX = CurPage / LinkNum;
			else
				CurPageX = CurPage / LinkNum + 1;

			if (CurPageX > 1)
				Response.Write("<a href=\"javascript:GoPage(" + (CurPageX - 1) * LinkNum + ")\" ><<</a>");

			for (int j = (CurPageX - 1) * LinkNum + 1; j <= PageNum && j <= CurPageX * LinkNum; j++)
			{
				if (j == CurPage)
					Response.Write("<a href=\"#\" class=\"curPage\">" + j.ToString() + "</a>");
				else
					Response.Write("<a href=\"javascript:GoPage(" + j + ")\" >" + j.ToString() + "</a>");
			}
			if (CurPageX < PageNumX)
				Response.Write("<a href=\"javascript:GoPage(" + (CurPageX * LinkNum + 1) + ")\" >>></a>");

			Response.Write("页,共" + PageNum + "页");
		}
	}
}